﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using elfam.web.Attributes;
using elfam.web.Exceptions;
using elfam.web.Models;
using elfam.web.Paging;
using elfam.web.Paging.Order;
using elfam.web.Services;
using elfam.web.ViewModels;
using Microsoft.Practices.Unity;
using NHibernate.Criterion;
using Order = elfam.web.Models.Order;

namespace elfam.web.Controllers
{
    public class OrderController : BaseController
    {
        OrderService orderService;

        [Dependency]
        public OrderService OrderService
        {
            set { orderService = value; }
        }
        
        [Admin]
        public ActionResult Details(string id)
        {
            // id is hash
            var order = daoTemplate.FindByUid(id);
            if (order == null)
                throw new NotFoundException();
            return View(order);
        }

        [Authorize]
        public ActionResult DetailsUser(string id)
        {
            var order = daoTemplate.FindByUid(id);
            if (order == null || !order.User.Equals(CurrentUser()))
                throw new NotFoundException();
            return View(order);
        }

//        [Admin]
//        public ActionResult List([DefaultValue(1)]int page, int? user)
//        {
//            DetachedCriteria criteria = DetachedCriteria.For(typeof (Order));
//            if (user.HasValue)
//            {
//                User userModel = daoTemplate.FindByID<User>(user.Value);
//                if (userModel != null)
//                    criteria.Add(Restrictions.Eq(Order.UserProperty, userModel));
//                else
//                {
//                    throw new NotFoundException();
//                }
//            }
//            PagingResult<Order> paging = Paging<Order>(criteria, page, 50);
//            return View(paging);
//        }

        [Admin]
        public ActionResult List(OrderListSearchCriteria criteria)
        {
            var queryBuilder = new OrderListQueryBuilder(criteria);
            var result = queryBuilder.Execute(daoTemplate.Session);
            return View(result);
        }

        [Admin]
        public ActionResult Revert(int id)
        {
            orderService.RevertOrder(daoTemplate.FindByID<Order>(id));
            return RedirectToAction("List");
        }

        [Admin]
        public ActionResult Update(int orderId, int status)
        {
            Order order = daoTemplate.FindByID<Order>(orderId);
            if (status == (int) OrderStatus.Revoked)
                return Revert(orderId);
            bool notify = ((OrderStatus) status) == order.Status;
            order.Status = (OrderStatus) status;
            orderService.UpdateUserDicount(order.User);
            if (notify)
                orderService.NotifyUser(order, ControllerContext);
            daoTemplate.Save(order);
            return RedirectToAction("Details", new {Id = order.Uid});
        }

        public ActionResult Bill(string id)
        {
            var order = daoTemplate.FindByUid(id);
            ViewData["price"] = order.SummWithDiscount();
            ViewData["address"] = order.ShippingDetails.AddressBill();
            ViewData["uid"] = order.Uid;
            ViewData["user_name"] = order.ShippingDetails.FullName;
            return View();
        }
    }

    
}
